import { LazyNumber } from "./types"
import { repeat } from "./common";
import { within, relative } from "./number"


///////////////////////// Square Root [BASIC] /////////////////////////

const next =
    (n: LazyNumber) =>
        (x: LazyNumber): LazyNumber =>
            () => ((x() + n() / x()) / 2);

const sqrt =
    (a0: LazyNumber) =>
        (eps: LazyNumber) =>
            (n: LazyNumber) =>
                within(eps)(repeat(next(n))(a0))

///////////////////////// Square Root [RELATIVE] /////////////////////////

const relativeSqrt =
    (a0: LazyNumber) =>
        (eps: LazyNumber) =>
            (n: LazyNumber): LazyNumber =>
                relative(eps)(repeat(next(n))(a0))


///////////////////////// TEST /////////////////////////

const a0 = () => 2;
const eps = () => 0.001;
const n = () => 10;

console.log(
    `sqrt: `,
    sqrt(a0)(eps)(n)()
)

console.log(
    `relativeSqrt: `,
    relativeSqrt(a0)(eps)(n)()
)

